package ServletYiChang;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

/**
 * @author 李某
 * * @date 2021/11/20
 * *物有本末，事有终始。知其先后者，则近道矣！
 * *荷花开满池塘的前一天还是半塘荷花半塘云。
 */
public class ServletErrorTest {}

//导入必需的 java 库
//扩展 HttpServlet 类

class ErrorHandler extends HttpServlet {

    // 处理 GET 方法请求的方法
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        Throwable throwable = (Throwable)
                request.getAttribute("javax.servlet.error.exception");
        Integer statusCode = (Integer)
                request.getAttribute("javax.servlet.error.status_code");
        String servletName = (String)
                request.getAttribute("javax.servlet.error.servlet_name");
        if (servletName == null){
            servletName = "Unknown";
        }
        String requestUri = (String)
                request.getAttribute("javax.servlet.error.request_uri");
        if (requestUri == null){
            requestUri = "Unknown";
        }
// 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String title = "菜鸟教程 Error/Exception 信息";
        String docType = "<!DOCTYPE html>\n";
        out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n");
        out.println("<h1>菜鸟教程异常信息实例演示</h1>");
        if (throwable == null && statusCode == null){
            out.println("<h2>错误信息丢失</h2>");
            out.println("请返回 <a href=\"" +
                    response.encodeURL("http://localhost:8080/") +
                    "\">主页</a>。");
        }else if (statusCode != null) {
            out.println("错误代码 : " + statusCode);
        }else{
            out.println("<h2>错误信息</h2>");
            out.println("Servlet Name : " + servletName +
                    "</br></br>");
            out.println("异常类型 : " +
                    throwable.getClass( ).getName( ) +
                    "</br></br>");
            out.println("请求 URI: " + requestUri +
                    "<br><br>");
            out.println("异常信息: " +
                    throwable.getMessage( ));
        }
        out.println("</body>");
        out.println("</html>");
    }
    // 处理 POST 方法请求的方法
    @Override
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
//  以通常的方式编译 ErrorHandler.java，把您的类文件放入/webapps/ROOT/WEB-INF/classes 中。
//  让我们在 web.xml 文件中添加如下配置来处理异常：

/**
 * <?xml version="1.0" encoding="UTF-8"?>
 * <web-app>
 * <servlet>
 *         <servlet-name>ErrorHandler</servlet-name>
 *         <servlet-class>com.runoob.test.ErrorHandler</servlet-class>
 * </servlet>
 * <!-- servlet mappings -->
 * <servlet-mapping>
 *         <servlet-name>ErrorHandler</servlet-name>
 *         <url-pattern>/TomcatTest/ErrorHandler</url-pattern>
 * </servlet-mapping>
 *
 * <error-page>
 *     <error-code>404</error-code>
 *     <location>/TomcatTest/ErrorHandler</location>
 * </error-page>
 *
 * <error-page>
 *     <exception-type>java.lang.Throwable</exception-type >
 *     <location>/ErrorHandler</location>
 * </error-page>
 * </web-app>
 */